﻿#include "mainwidget.h"
#include "ui_mainwidget.h"
#include <QLabel>
#include <QMouseEvent>
#include <QMessageBox>
#include <QDebug>

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    //    setWindowFlags(Qt::FramelessWindowHint);//隐藏边框和标题栏
    isLeftPressDown = false;
    this->dir = NONE;
    //    this->setMinimumHeight(100);
    //    this->setMinimumWidth(150);
    this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowSystemMenuHint );
    this->setMouseTracking(true);
    //    this->setStyleSheet("QDialog{background:url(:/bg_main.png)}");

    initGridLayout();

    //设置按钮样式
    ui->close->setStyleSheet("QPushButton{border-image: url(:/images/windows_close_regular.png);}"
                             "QPushButton:hover{border-image: url(:/images/windows_close_hovered.png);}"
                             "QPushButton:pressed{border-image: url(:/images/windows_close_pressed.png);}");
    ui->max->setStyleSheet("QPushButton{border-image: url(:/images/windows_de-maximize_regular.png);}"
                           "QPushButton:hover{border-image: url(:/images/windows_de-maximize_hovered.png);}"
                           "QPushButton:pressed{border-image: url(:/images/windows_de-maximize_pressed.png);}");
    ui->min->setStyleSheet("QPushButton{border-image: url(:/images/windows_minimize_regular.png);}"
                           "QPushButton:hover{border-image: url(:/images/windows_minimize_hovered.png);}"
                           "QPushButton:pressed{border-image: url(:/images/windows_minimize_pressed.png);}");
    ui->btnPopMenu->setStyleSheet("QPushButton{border-image: url(:/images/popMenu.png);}"
                                  "QPushButton:hover{border-image: url(:/images/popMenu2.png);}"
                                  "QPushButton:pressed{border-image: url(:/images/popMenu2.png);}");

    connect(ui->close, SIGNAL(clicked()), this, SLOT(windowclosed()));
    connect(ui->min, SIGNAL(clicked()), this, SLOT(windowmin()));
    connect(ui->max, SIGNAL(clicked()), this, SLOT(winmax()));

    QRect rcLedBoard = ui->widgetLedBoard->geometry();
    rcLedBoard.setWidth(400);
    ui->widgetLedBoard->setGeometry(rcLedBoard);
}

MainWidget::~MainWidget()
{
    delete ui;
}


void MainWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton) {
        isLeftPressDown = false;
        //if(dir != NONE) {
        this->releaseMouse();
        this->setCursor(QCursor(Qt::ArrowCursor));
        //}
    }
}

void MainWidget::mousePressEvent(QMouseEvent *event)
{
    switch(event->button()) {
    case Qt::LeftButton:
        isLeftPressDown = true;
        if(dir != NONE) {
            this->mouseGrabber();
        } else {
            dragPosition = event->globalPos() - this->frameGeometry().topLeft();
        }
        break;
    case Qt::RightButton:
        this->close();
        break;
    default:
        QWidget::mousePressEvent(event);
    }
}

void MainWidget::mouseMoveEvent(QMouseEvent *event)
{
    QPoint gloPoint = event->globalPos();
    QRect rect = this->rect();
    QPoint tl = mapToGlobal(rect.topLeft());
    QPoint rb = mapToGlobal(rect.bottomRight());

    if(!isLeftPressDown) {
        this->region(gloPoint);
    } else {

        if(dir != NONE) {
            QRect rMove(tl, rb);

            switch(dir) {
            case LEFT:
                if(rb.x() - gloPoint.x() <= this->minimumWidth())
                    rMove.setX(tl.x());
                else
                    rMove.setX(gloPoint.x());
                break;
            case RIGHT:
                rMove.setWidth(gloPoint.x() - tl.x());
                break;
            case UP:
                if(rb.y() - gloPoint.y() <= this->minimumHeight())
                    rMove.setY(tl.y());
                else
                    rMove.setY(gloPoint.y());
                break;
            case DOWN:
                rMove.setHeight(gloPoint.y() - tl.y());
                break;
            case LEFTTOP:
                if(rb.x() - gloPoint.x() <= this->minimumWidth())
                    rMove.setX(tl.x());
                else
                    rMove.setX(gloPoint.x());
                if(rb.y() - gloPoint.y() <= this->minimumHeight())
                    rMove.setY(tl.y());
                else
                    rMove.setY(gloPoint.y());
                break;
            case RIGHTTOP:
                rMove.setWidth(gloPoint.x() - tl.x());
                if(rb.y() - gloPoint.y() <= this->minimumHeight())
                    rMove.setY(tl.y());
                else
                    rMove.setY(gloPoint.y());
                break;
                /*rMove.setWidth(gloPoint.x() - tl.x());
                rMove.setY(gloPoint.y());
                break;*/
            case LEFTBOTTOM:
                rMove.setHeight(gloPoint.y() - tl.y());
                if(rb.x() - gloPoint.x() <= this->minimumWidth())
                    rMove.setX(tl.x());
                else
                    rMove.setX(gloPoint.x());
                break;
                /*rMove.setX(gloPoint.x());
                rMove.setHeight(gloPoint.y() - tl.y());
                break;*/
            case RIGHTBOTTOM:
                rMove.setWidth(gloPoint.x() - tl.x());
                rMove.setHeight(gloPoint.y() - tl.y());
                break;
            default:

                break;
            }
            this->setGeometry(rMove);
        } else {
            move(event->globalPos() - dragPosition);
            event->accept();
        }
    }
    QWidget::mouseMoveEvent(event);
}

void MainWidget::region(const QPoint &cursorGlobalPoint)
{
    QRect rect = this->rect();
    QPoint tl = mapToGlobal(rect.topLeft());
    QPoint rb = mapToGlobal(rect.bottomRight());
    int x = cursorGlobalPoint.x();
    int y = cursorGlobalPoint.y();

    if(tl.x() + PADDING >= x && tl.x() <= x && tl.y() + PADDING >= y && tl.y() <= y)
    {
        // 左上角
        dir = LEFTTOP;
        this->setCursor(QCursor(Qt::SizeFDiagCursor));
    } else if(x >= rb.x() - PADDING && x <= rb.x() && y >= rb.y() - PADDING && y <= rb.y())
    {
        // 右下角
        dir = RIGHTBOTTOM;
        this->setCursor(QCursor(Qt::SizeFDiagCursor));
    } else if(x <= tl.x() + PADDING && x >= tl.x() && y >= rb.y() - PADDING && y <= rb.y())
    {
        //左下角
        dir = LEFTBOTTOM;
        this->setCursor(QCursor(Qt::SizeBDiagCursor));
    } else if(x <= rb.x() && x >= rb.x() - PADDING && y >= tl.y() && y <= tl.y() + PADDING)
    {
        // 右上角
        dir = RIGHTTOP;
        this->setCursor(QCursor(Qt::SizeBDiagCursor));
    } else if(x <= tl.x() + PADDING && x >= tl.x())
    {
        // 左边
        dir = LEFT;
        this->setCursor(QCursor(Qt::SizeHorCursor));
    } else if( x <= rb.x() && x >= rb.x() - PADDING)
    {
        // 右边
        dir = RIGHT;
        this->setCursor(QCursor(Qt::SizeHorCursor));
    }else if(y >= tl.y() && y <= tl.y() + PADDING)
    {
        // 上边
        dir = UP;
        this->setCursor(QCursor(Qt::SizeVerCursor));
    } else if(y <= rb.y() && y >= rb.y() - PADDING)
    {
        // 下边
        dir = DOWN;
        this->setCursor(QCursor(Qt::SizeVerCursor));
    }
    else
    {
        // 默认
        dir = NONE;
        this->setCursor(QCursor(Qt::ArrowCursor));
    }
}

void MainWidget::initGridLayout()
{
}

void MainWidget::resizeEvent(QResizeEvent *event)
{
//    QRect rcFramePreview = ui->framePreview->geometry();
//    QRect rcLedBoard = ui->widgetLedBoard->geometry();
//    QRect rcPlayWindow = ui->label_PlayWindow->geometry();

//    //调用父类的resizeEvent函数
//    int layoutLeftMargin, layoutRightMargin, layoutTopMargin, layoutBottomMargin;
//    ui->framePreview->getContentsMargins(&layoutLeftMargin, &layoutRightMargin, &layoutTopMargin, &layoutBottomMargin);

//    int nPosX = (rcFramePreview.width() - rcLedBoard.width()) / 2;
//    int nPosY = (rcFramePreview.height() - ui->label_PlayWindow->height() - layoutTopMargin * 2 - layoutBottomMargin * 2 - rcLedBoard.height()) / 2;

//    rcLedBoard.setWidth(400);
//    ui->widgetLedBoard->move(rcLedBoard.left() + nPosX, rcLedBoard.top());
//    ui->label_PlayWindow->move(rcPlayWindow.left(), rcPlayWindow.top());

//    qDebug() << "rcFramePreview:" << rcFramePreview << "rcLedBoard:" << rcLedBoard;

    QWidget::resizeEvent(event);
}

void MainWidget::changeEvent(QEvent *e)
{
    //    int gridLayoutBoxLeft_width = ui->frameBox_Left->width();
    //    int gridLayoutBoxLeft_height = ui->frameBox_Left->height();

    //    qDebug()<< "changeEvent: gridLayoutBoxLeft_width: " << gridLayoutBoxLeft_width << ", gridLayoutBoxLeft_height: " << gridLayoutBoxLeft_height;
}

void MainWidget::moveEvent(QMoveEvent *e)
{
}

//槽函数实现
void MainWidget::windowclosed()
{
    if( QMessageBox::question(this,
                              tr("Quit"),
                              tr("Are you sure to quit this application?"),
                              QMessageBox::Yes, QMessageBox::No )
        == QMessageBox::Yes){

        close();//不会将事件传递给组件的父组件
    }
}

void MainWidget::windowmin()
{
    this->showMinimized();//最小化
}

void MainWidget::winmax()
{
    if (this->isMaximized())
    {
        this->showNormal();//还原事件
        //        maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton);
        //        maxButton->setIcon(maxPix);
        //        maxButton->setToolTip(tr("最大化"));
    }
    else
    {
        this->showMaximized();//最大化事件
        //        restore = style()->standardPixmap(QStyle::SP_TitleBarNormalButton);
        //        maxButton->setIcon(restore);
        //        maxButton->setToolTip(tr("最大化还原"));
    }
}
